<?php


$title = '<title>YEP5问答网 - 最全的知识问答网站</title>';
$keywords = '<meta name="keywords" content="YEP5,问答网,为什么,找答案，问答系统 提问 回答 知识搜索 "/>';
$description = '<meta name="description" content="最全的知识问答网站，用户可以搜索其他用户沉淀的精彩内容，浏览到最精彩的问答内容."/>';

$f3=require('../lib/base.php');

$f3->set('DEBUG',0);
$f3->set('UI','../views/');

$f3->config('../setup.cfg');

$f3->route('GET /',
           function($f3) {
               $host = 'http://wenwen.soso.com';
               $req = \Web::instance()->request($host);

               if ($req) {
                   $raw = $req['body'];
                   
                   $raw = preg_replace('/<span>[0-9]+回答<\/span>/i','',preg_replace('/[\r\t\n]*/i', '',$raw));
                   $raw = preg_replace('/<span>[0-9]+分<\/span>/i', '',$raw);
                   $raw = preg_replace('/\/z\/q([0-9]*)\.htm/i','/questions/q$1.html',$raw);
                   $raw = preg_replace('/\/z\/c([0-9]*)\.htm/i','/categories/c$1.html',$raw);

                   $rh1 = '/<ol class="question_list" ch="ww.home.djjwt">[\s\S]*?<\/ol>/i';
                   $rh2 = '/<ol class="question_list" ch="ww.home.gxswt">[\s\S]*?<\/ol>/i';
                   $rh3 = '/<ol class="question_list" ch="ww.home.jhzs">[\s\S]*?<\/ol>/i';


                   preg_match_all($rh1, $raw,$hot1);

                   preg_match_all($rh2, $raw,$hot2);
                   preg_match_all($rh3, $raw, $hot3);
                   $hot3 = preg_replace('/\/z\/ShowCategoryAutoAnswer\.e\?sp=([0-9]*)/i','/categories/c$1.html',$hot3[0][0]);

                   $f3->set('hot1', $hot1[0][0]);
                   $f3->set('hot2', $hot2[0][0]);
                   $f3->set('hot3', $hot3);

               }

               $f3->set("css", "home.css");
               echo View::instance()->render('home.html');
           },3600
    );

$f3->route('GET /categories/*', 
           function ($f3) {
               $url = $f3->get("PARAMS")[1];
               $this_link = "/categories/".$url;

               $url = preg_replace('/^c([0-9]*)\.html(.*)$/i',"s$1.htm$2",$url);

               $url = "http://wenwen.soso.com/z/".$url;
               $hiddenarr =  Web\Url::instance()->parseQuery($url);
               $hidden = false; 
               if (isset($hiddenarr['search']))
                   $hidden = ['search'] == 'hidden';

               $breadcrumbs = "";
               $category = "";

               $req = \Web::instance()->request($url);

               if ($req) {
                   preg_match('/<div class="lib_content">[\s\S]*?<div class="lib_side">/i', $req['body'], $raw);
                   preg_match('/<div style="float:left;" id="questionCategory">[\s\S]*?<\/div>/i', $req['body'],$breadcrumbs);
                   preg_match('/<title>[\s\S]*?<\/title>/i', $req['body'], $title);
                   $title = str_replace('搜搜问问','YEP5问答网',$title[0]);
                   preg_match('/<meta name="keywords" content=".*"><\/meta>/i', $req['body'],$keywords);
                   $keywords = str_replace(array("问问 搜搜 腾讯","></meta>"),array("YEP5 问答网", "/>"), $keywords[0]);

                   $description = '<meta name="description" content="最全的知识问答网站，用户可以搜索其他用户沉淀的精彩内容，浏览到最精彩的问答内容."/>';

                   $f3->set("site_name", $title);
                   $f3->set("site_desc", $description);
                   $f3->set("site_keywords", $keywords);

                   if ($breadcrumbs !== null) {
                       $breadcrumbs = str_replace('/z/TopQuestion.htm','/', $breadcrumbs);
                       $breadcrumbs = preg_replace('/\n\r/i', '',$breadcrumbs[0]);

                       $breadcrumbs = preg_replace('/\/z\/c([0-9]*)\.htm/i', '/categories/c$1.html', $breadcrumbs);
                       $links = array();
                       preg_match_all('/<a href="(.*?)">(.*?)<\/a>/i', $breadcrumbs,$bbs);
                       for($i=0; $i< count($bbs[0]); $i++) {
                           $links[$i]["href"] = $bbs[1][$i];
                           $links[$i]["text"] = $bbs[2][$i];
                       }

                       preg_match_all('/.*<\/span>(.*?)<\/div>$/i', $breadcrumbs, $lastitem);
                       $idx = count($links);
                       $links[$idx]["href"] = $this_link;
                       $links[$idx]["text"] = $lastitem[1][0];
                       $breadcrumbs = Web\Url::instance()->generateBreadcrumbs($links);
                   }

                   $raw_cate = str_replace('<div class="lib_side">','',$raw[0]);
                   $raw_cate = preg_replace('/<ul class="list_tit">[\s\S]*?<\/ul>/i','',$raw_cate);
                   $raw_cate = preg_replace('/\/z\/q([0-9]*)\.htm/i','/questions/q$1.html',$raw_cate);
                   $raw_cate = preg_replace('/\/z\/s([0-9]*)\.htm/i','/categories/c$1.html',$raw_cate);
                   $raw_cate = preg_replace('/\/z\/c([0-9]*)\.htm/i','/categories/c$1.html',$raw_cate);
                   $raw_cate = preg_replace('/\/z\/ShowTagSolved\.e\?sp=([0-9]*)\&amp;sp=S(.*)\&amp;/i','/categories/c$1.html?sw=$2&search=hidden&',$raw_cate);
                   $raw_cate = preg_replace('/<div class="lib_tab">[\s\S]*?<\/div>/i','', $raw_cate);
                   $raw_cate = preg_replace('/<li class="list_title">[\s\S]*?<\/li>/i','', $raw_cate);
                   $raw_cate = preg_replace('/<script[\s\S]*?<\/script>/i','', $raw_cate);
                   $raw_cate = str_replace('?ch=wtk.title','', $raw_cate);

                   $category = $raw_cate;
               }

               $f3->set("breadcrumbs",$breadcrumbs);
               $f3->set("category",$category);
               $f3->set("css", "category.css");
               echo View::instance()->render('category.html');

           },3600
    );

$f3->route('GET /questions/*',
           function($f3) {
               $breadcrumbs = '';
               $questions = '';

               $url = $f3->get("PARAMS")[1];

               $url = preg_replace('/q([0-9]+)\.html(.*)/i',"http://wenwen.soso.com/z/q$1.htm$2",$url);

               $req = \Web::instance()->request($url);

               if ($req) {
                   preg_match('/<div class="column1">[\s\S]*?<div class="column2">/i', $req['body'], $raw);
                   preg_match('/<div class="breadcrumbs">[\s\S]*?<\/div>/i', $req['body'],$breadcrumbs);

                   if (isset($breadcrumbs[0])) {
                       $breadcrumbs = preg_replace('/<script[\s\S]*?<\/script>/i','', $breadcrumbs[0]);
                       $breadcrumbs = str_replace('/z/TopQuestion.htm','/', $breadcrumbs);
                       $breadcrumbs = preg_replace('/\n\r/i', '',$breadcrumbs);

                       $breadcrumbs = preg_replace('/\/z\/c([0-9]*)\.htm/i', '/categories/c$1.html', $breadcrumbs);
                       $links = array();
                       preg_match_all('/<a href="(.*?)">(.*?)<\/a>/i', $breadcrumbs,$bbs);
                       for($i=0; $i< count($bbs[0]); $i++) {
                           $links[$i]["href"] = $bbs[1][$i];
                           $links[$i]["text"] = $bbs[2][$i];
                       }

                       $breadcrumbs = Web\Url::instance()->generateBreadcrumbs($links);
                   }


                   
                   preg_match('/<title>[\s\S]*?<\/title>/i', $req['body'], $title);
                   $title = str_replace('搜搜问问','YEP5问答网',$title[0]);
                   preg_match('/<meta name="keywords" content=".*"><\/meta>/i', $req['body'],$keywords);
                   if (isset($keywords[0]))
                       $keywords = str_replace(array("问问 搜搜 腾讯","></meta>"),array("YEP5 问答网", "/>"), $keywords[0]);
                   else 
                       $keywords = $f3->get('site_keywords');


                   $description = '<meta name="description" content="最全的知识问答网站，用户可以搜索其他用户沉淀的精彩内容，浏览到最精彩的问答内容."/>';

                   $raw = str_replace('<div class="column2">', '',$raw);

                   $f3->set("site_name", $title);
                   $f3->set("site_desc", $description);
                   $f3->set("site_keywords", $keywords);
                   if (isset($raw[0])) {
                       $questions = preg_replace('/<script[\s\S]*?<\/script>/i','', $raw[0]);
                       $questions = preg_replace('/[\n\r\t]/i','',$questions);
                       $questions = preg_replace('/<img src="http:\/\/pic\.wenwen.*?">/i','',$questions);
                       $questions = str_replace('resolved_question','  resolved_question', $questions);
                       $questions = preg_replace('/<form[\s\S]*?<\/form>/i','', $questions);
                       $questions = preg_replace('/<a id="firstAnswerBoxBtn[\s\S]*?<\/a>/i','', $questions);
                       $questions = preg_replace('/<div class="vote_con.*?<\/div>/i','', $questions);
                       $questions = preg_replace('/<div class="share_other">[\s\S].*?<\/div>/i','', $questions);
                       $questions = preg_replace('/<div class="user_pic[\n\r\s\S]*?<\/div>/i', '<div class="user_pic"><a target="_blank" href="'.$f3->get('twin_site_url').'"><img src="/assets/img/avatar.png" width="40px" height="40px"/></a></div>',$questions);

                       $questions = preg_replace('/<div class="expansion_other_wrap">[\s\S].*?<\/div>/i','', $questions);
                       $questions = preg_replace('/<div class="ad1 mar_top.*?<\/div>/i','', $questions);

                       $questions = preg_replace('/<a href="\/z\/ShowUser.*?<\/a>/i','<a href="'.$f3->get('twin_site_url').'" target="_blank" class="user_name">'.$f3->get('twin_site_name').'</a>', $questions);
                       $questions = preg_replace('/<a onclick=".*?<\/a>/i','',$questions);
                       $questions = preg_replace('/onclick=".*?"/i','',$questions);
                       $questions = preg_replace('/<a target="_blank" id="ans_user_card_name[0-9]*" href="\/t\/t.*?<\/a>/i','<a href="'.$f3->get('twin_site_url').'" target="_blank" class="user_name">'.$f3->get('twin_site_name').'</a>', $questions);

                       $questions = preg_replace('/<a href="\/z\/ShowUser.*?<\/a>/i','<a href="'.$f3->get('twin_site_url').'" target="_blank" class="user_name">'.$f3->get('twin_site_name').'</a>', $questions);
/*

  


                       $questions = preg_replace('/<div id="ans_user_card.*?<\/div>/i','', $questions);

                       $questions = preg_replace('/<div class="user_sign.*?<\/div>/i','', $questions);
                       $questions = preg_replace('/<ul class="mod_card_list.*?<\/ul>/i','', $questions);
                   

                       
                   
*/                     
                   }
               }

               $f3->set('breadcrumbs',$breadcrumbs);
               $f3->set('questions', $questions);
               $f3->set('css', 'question.css');
               echo View::instance()->render('question.html');
           },3600
    );

$f3->run();
